package com.un.ebs.workflow.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.un.ebs.contract.dto.ContractVo;
import com.un.ebs.core.util.SecurityUtils;
import com.un.ebs.core.util.StringUtils;
import com.un.ebs.core.util.WebResult;
import com.un.ebs.core.util.WebUtils;
import com.un.ebs.inventory.enums.InventoryTypeEnum;
import com.un.ebs.workflow.domain.*;
import com.un.ebs.workflow.dto.*;
import com.un.ebs.workflow.enums.NodePassTypeEnum;
import com.un.ebs.workflow.service.WorkflowInstanceService;
import com.un.ebs.workflow.service.WorkflowService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Workflow)表控制层
 *
 * @author liubo
 * @since 2020-08-23 09:56:25
 */
@RestController
@RequestMapping("workflow")
@Api(value = "workflow")
public class WorkflowController {
    /**
     * 服务对象
     */
    @Resource
    private WorkflowService workflowService;

    @Resource
    private WorkflowInstanceService workflowInstanceService;

    @PostMapping("update")
    @ApiOperation(value = "保存")
    public WebResult update(@RequestBody WorkflowUpdateDto req) {
        workflowService.update(req);
        return WebUtils.success();
    }

    @PostMapping("updateNode")
    @ApiOperation(value = "保存")
    public WebResult updateNode(@RequestBody WorkflowNodeUpdateDto req) {
        workflowService.updateNode(req);
        return WebUtils.success();
    }

    @PostMapping("updateGateway")
    @ApiOperation(value = "保存")
    public WebResult updateGateway(@RequestBody WorkflowNodeGatewayUpdateDto req) {
        workflowService.updateGateway(req);
        return WebUtils.success();
    }

    @PostMapping("delete")
    @ApiOperation(value = "删除")
    public WebResult delete(@RequestParam String id) {
        workflowService.delete(id);
        return WebUtils.success();
    }

    @PostMapping("deleteNode")
    @ApiOperation(value = "删除")
    public WebResult deleteNode(@RequestParam String id) {
        workflowService.deleteNode(id);
        return WebUtils.success();
    }

    @PostMapping("deleteGateway")
    @ApiOperation(value = "删除")
    public WebResult deleteGateway(@RequestParam String id) {
        workflowService.deleteGateway(id);
        return WebUtils.success();
    }

    @GetMapping("listNodeById")
    @ApiOperation(value = "查询")
    public WebResult<WorkflowNodeVo> queryAllNodeById(@RequestParam String workflowId) {
        return WebUtils.success(workflowService.queryAllNodeById(workflowId));
    }

    @GetMapping("node/{id}")
    @ApiOperation(value = "节点详情")
    public WebResult<WorkflowNodeExt> queryNodeById(@PathVariable String id) {
        return WebUtils.success(workflowService.queryNodeById(id));
    }

    @GetMapping("list")
    @ApiOperation(value = "查询流程定义")
    public WebResult<IPage<Workflow>> query(@RequestParam String searchText, @RequestParam int pageSize, @RequestParam int pageIndex) {
        return WebUtils.success(workflowService.query(StringUtils.isNullOrEmpty(searchText) ? null : searchText.trim(), pageSize, pageIndex));
    }

    @GetMapping("listInstance")
    @ApiOperation(value = "查询流程实例")
    public WebResult<IPage<WorkflowInstance>> queryInstance(@RequestParam String workflowId, @RequestParam int pageSize, @RequestParam int pageIndex) {
        return WebUtils.success(workflowInstanceService.query(workflowId,pageSize,pageIndex));
    }

    @GetMapping("listNodes")
    @ApiOperation(value = "查询流程定义")
    public WebResult<IPage<WorkflowNodeExt>> queryNodes(@RequestParam String workflowId) {
        return WebUtils.success(workflowService.queryNodes(workflowId));
    }

    @GetMapping("listGateways")
    @ApiOperation(value = "查询流程定义")
    public WebResult<IPage<Workflow>> queryGateway(@RequestParam String workflowId) {
        return WebUtils.success(workflowService.queryGatewayByWorkFlowId(workflowId));
    }

    @GetMapping("listMyTask")
    @ApiOperation(value = "查询")
    public WebResult<List<WorkflowNodeInstanceExt>> queryMyTask() {
        return WebUtils.success(workflowInstanceService.queryCurrentTask(SecurityUtils.getUserContent().getId()));
    }

    @GetMapping("listNodeInstance")
    @ApiOperation(value = "查询实例所有记录")
    public WebResult<List<WorkflowNodeInstanceExt>> queryTask(@RequestParam String workflowInstanceId) {
        return WebUtils.success(workflowInstanceService.queryTask(workflowInstanceId));
    }

    @PostMapping("start")
    @ApiOperation(value = "开始流程实例")
    public WebResult start(@RequestParam String workflowId) {
        return WebUtils.success(workflowInstanceService.start(workflowId, null));
    }

    @PostMapping("push")
    @ApiOperation(value = "开始流程实例")
    public WebResult push(@RequestParam String nodeInstanceId, @RequestParam String remark, @RequestParam NodePassTypeEnum type) {
        workflowInstanceService.taskPush(nodeInstanceId, remark, type);
        return WebUtils.success();
    }

//    @PostMapping("pushByContract")
//    @ApiOperation(value = "开始流程实例")
//    public WebResult pushByContract(@RequestBody WorkflowInstanceNodePushRequest req) {
//        //workflowInstanceService.taskPush(nodeInstanceId, remark, type);
//        workflowInstanceService.taskPushByContract(req);
//        return WebUtils.success();
//    }

    @PostMapping("close")
    @ApiOperation(value = "关闭实例")
    public WebResult close(@RequestParam String workflowInstanceId, @RequestParam String remark) {
        workflowInstanceService.close(workflowInstanceId, remark, false);
        return WebUtils.success();
    }

//    @GetMapping("queryTaskByContract")
//    @ApiOperation(value = "查询当前任务的业务对象")
//    public WebResult<WorkflowInstanceObject<ContractVo>> queryContract(@RequestParam String nodeInstanceId, @RequestParam String objectId, @RequestParam InventoryTypeEnum type){
//        return WebUtils.success(workflowInstanceService.queryObject(objectId,nodeInstanceId,type));
//    }

}